//
//  AddHeadImageCell.m
//  MIDI
//
//  Created by 邵子凡 on 15/11/23.
//  Copyright © 2015年 QXSX. All rights reserved.
//

#import "AddHeadImageCell.h"
#import "UIButton+WebCache.h"
#import "UserDataSingleton.h"
#import "UIImageView+WebCache.h"
#import "CommFunc.h"
#import "AppConfig.h"
#import "SelfInfoModel.h"
#import "AccountTool.h"

@interface AddHeadImageCell ()

//@property (weak, nonatomic) IBOutlet UIButton *headImageBtn;
@property (weak, nonatomic) IBOutlet UIImageView *titleIco;
@property (weak, nonatomic) IBOutlet UILabel *nickName;
@property (weak, nonatomic) IBOutlet UILabel *MIDI_ID;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrowIco;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headTopYConstraint;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headWidthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headHeightConstraint
;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleLabelRightConstraint
;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nickName_y_Constraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *midiId_y_Constraint;


@end

@implementation AddHeadImageCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
    
    static NSString *ID = @"AddHeadImageCell";
    
    AddHeadImageCell  *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"AddHeadImageCell" owner:nil options:nil]firstObject] ;
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

- (void)awakeFromNib {

    self.nickName.hidden = YES;
    self.MIDI_ID.hidden = YES;
    self.titleLabel.hidden = YES;

    self.headWidthConstraint.constant = [CommFunc getMyselfHeadImageWidth];
    self.headHeightConstraint.constant = [CommFunc getMyselfHeadImageHeight];
    
    self.headImage.userInteractionEnabled = YES;
    [self.headImage.layer setCornerRadius:self.headWidthConstraint.constant/2];
    self.headImage.contentMode = UIViewContentModeScaleToFill;
    self.headImage.clipsToBounds = YES;
    
    UserDataSingleton * userdata = [UserDataSingleton shareInstance];
    if([userdata isLogin])
    {
        //离线
        if (![CommFunc isConnectionAvailable])
        {
            self.nickName.hidden = NO;
            self.MIDI_ID.hidden = NO;
            self.titleLabel.hidden = NO;
            
            self.MIDI_ID.text = [AccountTool takeMidiId];
            self.nickName.text = [AccountTool takeNickName];
            self.titleLabel.text = @"离线状态";
            
            self.midiId_y_Constraint.constant = -3;
            
            return;
        }
        
        if(!userdata.isGotSelfInfo)
        {
            self.nickName.hidden = NO;
            self.MIDI_ID.hidden = NO;
            self.titleLabel.hidden = NO;
            
            self.MIDI_ID.text = [AccountTool takeMidiId];
            self.nickName.text = [AccountTool takeNickName];
            self.titleLabel.text = @"离线状态";
            
            self.midiId_y_Constraint.constant = -3;
            
            return;
        }
        
        //在线
        if(userdata.isGotSelfInfo)
        {
            [self.MIDI_ID setTextColor:[UIColor blackColor]];
            
            NSString *icoUrl = userdata.head_img;
            if(icoUrl == nil || [icoUrl isEqual:@""])
            {
                [self.headImage setImage:[UIImage imageNamed:@"ico_selfinfo_nonoe"]];
            }
            else
            {
                UIImage *tempImage = [userdata getLocalHeadImage];
                if (tempImage)
                {
                    //用选取的头像更换
                    [self.headImage setImage:tempImage];
                }
                else
                {
                    //从服务器下载
                    UIImageView *imageView = [[UIImageView alloc] init];
                    
                    [imageView sd_setImageWithURL:[NSURL URLWithString:userdata.head_img] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
                     {
                         [self.headImage setImage:imageView.image];
                     }];
                }
            }
            
            //昵称
            if (userdata.nickname != nil && ![userdata.nickname isEqualToString:@""])
            {
                self.nickName.text = userdata.nickname;
                self.nickName.hidden = NO;
            }
            
            //midi id
            if (userdata.midi_id != nil && ![userdata.midi_id isEqualToString:@""])
            {
                [self.MIDI_ID setText:[@"Midi ID: " stringByAppendingString:userdata.midi_id]];
                
                self.MIDI_ID.hidden = NO;
            }
            
            //身份
            self.titleLabel.text = @"";
            
//            如果ismidisir有值 & role_id = 100 显示 迷笛先生
//            如果ismidisir有值 & role_id = 101 根据level等级 显示 迷笛先生 & 迷笛考官（助理、初、中、高）
//            如果ismidisir有值 & role_id = 102 显示 迷笛先生&工作人员
//            
//            如果ismidisir为空 & role_id = 100 显示普通用户
//            如果ismidisir为空 & role_id = 101 根据level等级 显示迷笛考官 （助理、初、中、高）
//            如果ismidisir为空 & role_id = 102 显示工作人员

            NSString *show;
            if (userdata.ismidisir != nil && ![userdata.ismidisir isEqualToString:@""])
            {
                NSString *level;
                
                if ([userdata.ismidisir isEqualToString:@"6"]) {
                    level = @"初";
                }else if ([userdata.ismidisir isEqualToString:@"7"]){
                    level = @"中";
                }else if ([userdata.ismidisir isEqualToString:@"8"]) {
                    level = @"高";
                } else if ([userdata.ismidisir isEqualToString:@"9"]) {
                    level = @"特";
                }else {
                    level = @"";
                }
                
                //迷笛先生
                if ([userdata.role_id isEqualToString:@"100"])//普通用户
                {
                    show = [NSString stringWithFormat:@"迷笛先生(%@级)", level];
                }
                else if ([userdata.role_id isEqualToString:@"101"])
                {
                    //迷笛先生（初、中、高、特）&迷笛考官（助理、初、中、高）
                    show = [NSString stringWithFormat:@"迷笛先生(%@级)&迷笛考官(%@)", level, [self getExaminerLevelByLevel:userdata.level]];
                }
                else if ([userdata.role_id isEqualToString:@"102"])
                {
                    //迷笛先生&工作人员
                    show = [NSString stringWithFormat:@"迷笛先生(%@级)&工作人员", level];
                }
                
                self.titleLabel.text = show;
                self.titleLabel.hidden = NO;
            }
            else
            {
                self.titleLabel.hidden = NO;
                
                if ([userdata.role_id isEqualToString:@"100"])//普通用户
                {
                    if ([UIScreen mainScreen].applicationFrame.size.width==320)
                    {
                        if([UIScreen mainScreen].applicationFrame.size.height == 460)
                        {
                            //4s
                            self.titleLabelRightConstraint.constant = 110;
                        }
                    }
                    
                    NSMutableAttributedString *attAry = [[NSMutableAttributedString alloc] init];
                    
                    if ([userdata.majorList count] == 0)
                    {
                        self.nickName_y_Constraint.constant += 8;
                        self.midiId_y_Constraint.constant -= 10;
                        
                        self.titleLabel.hidden = YES;
                    }
                    else
                    {
                        for (int i = 0; i < [userdata.majorList count]; ++i)
                        {
                            MajorListOneModel *one = [userdata.majorList objectAtIndex:i];
                            if (one && ![one.major_id isEqualToString:[UserDataSingleton shareInstance].musicBase])
                            {
                                NSMutableAttributedString *item = [[NSMutableAttributedString alloc] init];
                                
                                //专业图片
                                NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];
                                
                                NSString *professionImage = [self getProfessionImageByMajorId:one.major_id];
                                
                                textAttachment.image = [UIImage imageNamed:professionImage];
                                
                                NSAttributedString *imageItem = [NSAttributedString attributedStringWithAttachment:textAttachment];
                                
                                //级别
                                NSString *levelText = [NSString stringWithFormat:@" %@级  ", one.grade_num];
                                
                                NSMutableAttributedString *levelItem = [[NSMutableAttributedString alloc] initWithString:levelText];
                                
                                //调节底部偏移量
                                [levelItem addAttribute:NSBaselineOffsetAttributeName value:@(2) range:NSMakeRange(0, levelItem.length)];
                                //
                                [item appendAttributedString:levelItem];
                                [item insertAttributedString:imageItem atIndex:0];
                                [attAry appendAttributedString:item];
                            }
                            
                            self.titleLabel.attributedText = attAry.copy;
                        }
                    }
                }
                else if ([userdata.role_id isEqualToString:@"101"])//显示迷笛考官 （助理、初、中、高）
                {
                    show = [NSString stringWithFormat:@"迷笛考官(%@)", [self getExaminerLevelByLevel:userdata.level]];
                    
                    self.titleLabel.text = show;
                }
                else if ([userdata.role_id isEqualToString:@"102"])//工作人员
                {
                    self.titleLabel.text = @"工作人员";
                }
            }
        }
    }
    else
    {
        self.titleIco.hidden = YES;
        self.titleLabel.hidden = YES;
        self.MIDI_ID.hidden = NO;
        [self.MIDI_ID setText:@"登陆/注册"];
        [self.MIDI_ID setTextColor:UIColorRGBA(19, 208, 202, 1)];
        
        [self.headImage setImage:[UIImage imageNamed:@"ico_selfinfo_nonoe"]];
    }
    

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headImageClick)];
    
    [self addGestureRecognizer:tapGestureRecognizer];
}

-(NSString *)getExaminerLevelByLevel:(NSString *)level
{
    if ([level isEqualToString:@"1"]) {
        return @"助理";
    } else if ([level isEqualToString:@"2"]){
        return @"初级";
    } else if ([level isEqualToString:@"3"]) {
        return @"中级";
    } else if ([level isEqualToString:@"4"]){
        return @"高级";
    }
    
    return @"";
}

-(NSString *)getProfessionImageByMajorId:(NSString *)majorId
{
    NSString *strImage = @"";
    
    if ([majorId isEqualToString: [UserDataSingleton shareInstance].quiTa])
    {
        strImage = @"ico_myself_head_profesion_dianjita";
    }
    else if ([majorId isEqualToString: [UserDataSingleton shareInstance].bass])
    {
        strImage = @"ico_myself_head_profesion_bass";
    }
    else if ([majorId isEqualToString: [UserDataSingleton shareInstance].JazzDrum])
    {
        strImage = @"ico_myself_head_profesion_jazz";
    }
    
    return strImage;
}

- (void)headImageClick{
    
    if ([self.delegete respondsToSelector:@selector(uploadHeadImageBtn) ]) {
        
        [self.delegete uploadHeadImageBtn];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)hideArrow
{
    self.arrowIco.hidden = YES;
}

@end













